home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include "movie.h"
- #include <cl.h>
- #include <qt.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
-
- #ifdef DEBUG
- #define dprintf printf
- #else
- #define dprintf 0&&
- #endif
-
-
- Movie::Movie()
- {
- dprintf("Movie\n");
- _movieID = _imageID = _audioID = NULL;
- _width = _height = 0;
- _numFrames = _numAudio = 0;
- _rate = 0.0;
- _movieName = NULL;
- _compressType = NULL;
- _optimizeMovie = FALSE;
- startFrame();
- }
-
- Movie::~Movie()
- {
- dprintf("end Movie\n");
- if(_movieName) free(_movieName);
- if(_compressType) free (_compressType);
- }
-
- void Movie::addAudio(Audio *audio)
- {
- DMparams *params;
- if(!audio) return;
- dprintf("addAudio\n");
- if(!getAudioID()){
- if(dmParamsCreate(¶ms) != DM_SUCCESS){
- fprintf(stderr,"Unable to create audio params.\n");
- return;
- }
- if(dmSetAudioDefaults(params, audio->getBytesPerSample() * 8,
- (double)audio->getRate(),
- audio->getNumChannels()) != DM_SUCCESS){
- fprintf(stderr,"Unable to setup audio defaults.\n");
- }
- if(dmParamsSetEnum(params,
- DM_AUDIO_FORMAT,
- DM_AUDIO_TWOS_COMPLEMENT) != DM_SUCCESS){
- fprintf(stderr,"Unable to set audio format\n");
- }
- if(mvAddTrack(_movieID, DM_AUDIO, params, NULL, &_audioID) != DM_SUCCESS){
- fprintf(stderr,"Unable to add image track.\n");
- }
- dmParamsDestroy(params);
- setNumAudio(0);
- }
-
- int numSamples;
- void *buf = audio->getAudio(numSamples);
- mvInsertFrames(_audioID, getNumAudio(), numSamples,
- numSamples * audio->getBytesPerSample(), buf);
- setNumAudio(getNumAudio() + numSamples);
- }
-
- void *Movie::getFrame(int frameNo)
- {
- void *buf;
- dprintf("getFrame\n");
- if(frameNo < 0) return NULL;
- if(frameNo >= getNumFrames()) return NULL;
- char *cbuf = new char[getFrameSize()];
- buf = (void *)cbuf;
- if(mvReadFrames(getImageID(), frameNo, 1, getFrameSize(), buf) != DM_SUCCESS){
- fprintf(stderr,"Cannot read frame %d\n",frameNo);
- return NULL;
- }
- return buf;
- }
-
- int Movie::setFrame(MVframe frameNo, void *buf)
- {
- MVframe currentFrame;
- dprintf("setFrame\n");
- currentFrame = (frameNo > 0) ? getNumFrames() : frameNo;
- if(mvInsertFrames(getImageID(), currentFrame, 1, getFrameSize(), buf) != DM_SUCCESS){
- fprintf(stderr,"Cannot insert frame %d\n",currentFrame);
- mvDeleteFrames(getImageID(), currentFrame-1, 1);
- return FALSE;
- }
- setNumFrames(mvGetTrackLength(getImageID()));
- return TRUE;
- }
-
- int Movie::setMovie(const char *name)
- {
- DMparams *params;
- dprintf("setMovie\n");
- if(getMovieID()){
- mvClose(getMovieID());
- setMovieID(NULL);
- }
- if(_movieName){
- free (_movieName);
- _movieName = NULL;
- }
- if(!name) return -1;
- _movieName = strdup(name);
- if(!mvIsMovieFile(_movieName)) {
- return -1;
- }
- if(mvOpenFile(_movieName,O_RDONLY,&_movieID) == DM_FAILURE){
- fprintf(stderr,"Cannot open movie %s\n",_movieName);
- return -1;
- }
- if(mvFindTrackByMedium(_movieID, DM_IMAGE, &_imageID) == DM_FAILURE){
- setImageID(NULL);
- }
- if(mvFindTrackByMedium(_movieID, DM_AUDIO, &_audioID) == DM_FAILURE){
- setAudioID(NULL);
- }
- if(getImageID() != NULL){
- _numFrames = mvGetTrackLength(_imageID);
- _width = mvGetImageWidth(_imageID);
- _height = mvGetImageHeight(_imageID);
- _rate = mvGetImageRate(_imageID);
- params = mvGetParams(_imageID);
- _frameSize = dmImageFrameSize(params);
- if(_compressType) free(_compressType);
- _compressType = strdup(mvGetImageCompression(_imageID));
- }else{
- _numFrames = _width = _height = _frameSize = 0;
- _rate = 0.0;
- if(_compressType) free(_compressType);
- _compressType = NULL;
- }
- if(_audioID != NULL){
- _numAudio = mvGetTrackLength(_audioID);
- }else{
- _numAudio = 0;
- }
- startFrame();
- return 0;
- }
-
- void Movie::setImageID(MVid m)
- {
- dprintf("setImageID\n");
- _imageID = m;
- if(m != NULL){
- _width = mvGetImageWidth(m);
- _height = mvGetImageHeight(m);
- _frameSize = dmImageFrameSize(mvGetParams(m));
- _compressType = strdup(mvGetImageCompression(m));
- _rate = mvGetImageRate(m);
- }else{
- _width = _height = _frameSize = 0;
- _numFrames = 0;
- _rate = 0.0;
- if(_compressType) free(_compressType);
- _compressType = NULL;
- }
- }
-
- void Movie::setAudioID(MVid m)
- {
- dprintf("setAudioID\n");
- _audioID = m;
- if(m != NULL){
- _numAudio = mvGetTrackLength(_audioID);
- }
- }
-
- void Movie::setMovieID(MVid m)
- {
- dprintf("setMovieID\n");
- _movieID = m;
- setAudioID(NULL);
- setImageID(NULL);
- _width = _height = 0;
- _numFrames = 0;
- _movieName = NULL;
- _rate = 0.0;
- _compressType = NULL;
- startFrame();
- }
-
- char *
- Movie::getCompressionType()
- {
- if(_compressType) return strdup(_compressType);
- return NULL;
- }
-
- char *
- Movie::getName()
- {
- if(_movieName) return strdup(_movieName);
- return NULL;
- }
-
-
- NewMovie::NewMovie(const char *name, double r, int w, int h, const char *c )
- {
- DMparams *params;
- dprintf("NewMovie\n");
- if(dmParamsCreate(¶ms) != DM_SUCCESS){
- fprintf(stderr,"Unable to create params.\n");
- }
- MVfileformat mformat = MV_FORMAT_SGI_3;
- if(strncmp("Apple ",c,6) == 0) mformat = MV_FORMAT_QT;
-
- if(mvSetMovieDefaults(params,mformat) != DM_SUCCESS){
- fprintf(stderr,"Unable to set params.\n");
- }
- if(mvCreateFile(name, params, NULL,&_movieID) != DM_SUCCESS){
- fprintf(stderr,"Unable to create file.\n");
- }
- dmParamsDestroy(params);
- _movieName = strdup(name);
- if(dmParamsCreate(¶ms) != DM_SUCCESS){
- fprintf(stderr,"Unable to create params.\n");
- }
- _width = w;
- _height = h;
- if(dmSetImageDefaults(params, w, h, DM_PACKING_RGBX) != DM_SUCCESS){
- fprintf(stderr,"Unable to setup image defaults.\n");
- }
- _frameSize = dmImageFrameSize(params);
- if(dmParamsSetString(params, DM_IMAGE_COMPRESSION, c) != DM_SUCCESS){
- fprintf(stderr,"Unable to set compression.\n");
- }
- _rate = r;
- if(dmParamsSetFloat(params, DM_IMAGE_RATE, r) != DM_SUCCESS){
- fprintf(stderr,"Unable to set rate.\n");
- }
- if(mvAddTrack(_movieID, DM_IMAGE, params, NULL,&_imageID) != DM_SUCCESS){
- fprintf(stderr,"Unable to add image track.\n");
- }
- dmParamsDestroy(params);
- if(_imageID) _compressType = strdup(mvGetImageCompression(_imageID));
- _numFrames = 0;
- startFrame();
- }
-
- NewMovie::NewMovie(Movie *oldMovie, const char *name)
- {
- MVid oldID;
- DMparams *params;
- dprintf("NewMovie\n");
- if(dmParamsCreate(¶ms) != DM_SUCCESS){
- fprintf(stderr,"Unable to create video params.\n");
- return;
- }
- oldID = oldMovie->getMovieID();
- MVfileformat mformat = MV_FORMAT_SGI_3;
- if(strncmp("Apple",oldMovie->getCompressionType(),6) == 0){
- mformat = MV_FORMAT_QT;
- }
- if(mvSetMovieDefaults(params,mformat) != DM_SUCCESS){
- fprintf(stderr,"Cannot set movie defaults,\n");
- return;
- }
- if(mvCreateFile(name,params,NULL,&_movieID) != DM_SUCCESS){
- fprintf(stderr,"Cannot create movie.\n");
- return;
- }
- if(mvSetLoopMode(_movieID,mvGetLoopMode(oldID)) != DM_SUCCESS){
- fprintf(stderr,"Cannot set loop mode.\n");
- return;
- }
- if(mvSetLoopLimit(_movieID,mvGetLoopLimit(oldID)) != DM_SUCCESS){
- fprintf(stderr,"Cannot set loop limit.\n");
- return;
- }
- dmParamsDestroy(params);
- _movieName = strdup(name);
- _width = oldMovie->getWidth();
- _height = oldMovie->getHeight();
- if(dmSetImageDefaults(params, _width, _height, DM_PACKING_RGBX) != DM_SUCCESS){
- fprintf(stderr,"Unable to setup image defaults.\n");
- }
- _frameSize = dmImageFrameSize(params);
- if(dmParamsSetString(params,
- DM_IMAGE_COMPRESSION,
- oldMovie->getCompressionType()) != DM_SUCCESS){
- fprintf(stderr,"Unable to set compression.\n");
- }
- if(mvAddTrack(_movieID, DM_IMAGE, params, NULL,&_imageID) != DM_SUCCESS){
- fprintf(stderr,"Unable to add image track.\n");
- }
- dmParamsDestroy(params);
- if(_imageID) _compressType = strdup(mvGetImageCompression(_imageID));
- _numFrames = 0;
- startFrame();
- }
-
- NewMovie::~NewMovie()
- {
- char str[128];
- Audio *audio;
- dprintf("end NewMovie\n");
- //
- // reset the frame rate to be the closest given the audio that
- // has been grabbed
- //
- if(getNumFrames() == 0) {
- mvClose(_movieID);
- unlink (_movieName);
- if(_movieName)free (_movieName);
- _movieName = NULL;
- if(_compressType) free(_compressType);
- _compressType = NULL;
- return;
- }
- audio = NULL;
- if(getNumAudio() > 0){
- audio = new Audio();
- double rate = getNumFrames() / ( (float)getNumAudio() / audio->getRate() );
- if(mvSetImageRate(_imageID, rate) != DM_SUCCESS){
- fprintf(stderr,"Cannot update image rate to %f\n",rate);
- }
- }
- if(mvGetFileFormat(_movieID) != MV_FORMAT_SGI_3){
- mvClose(_movieID);
- }else{
- if(_optimizeMovie){
- MVid optID;
- sprintf(str,"%s.tmp",_movieName);
- if(mvCreateFile(str,mvGetParams(_movieID),NULL,&optID) != DM_SUCCESS){
- fprintf(stderr,"Unable to open opt file.\n");
- mvClose(_movieID);
- }else{
- if(mvOptimize(_movieID, optID) != DM_SUCCESS){
- fprintf(stderr,"Unable to optimize movie.\n");
- fprintf(stderr,"Movie:%s\n",mvGetErrorStr(mvGetErrno()));
- mvClose(optID);
- mvClose(_movieID);
- unlink(str);
- }else{
- mvClose(optID);
- mvClose(_movieID);
- rename(str,_movieName);
- }
- }
- }else{
- mvClose(_movieID);
- }
- }
- if(_movieName)free (_movieName);
- _movieName = NULL;
- if(_compressType) free(_compressType);
- _compressType = NULL;
- delete audio;
- }
-
- OldMovie::OldMovie(const char *name)
- {
- dprintf("OldMovie\n");
- if(setMovie(name) < 0){
- _movieName = NULL;
- }
- }
-
- OldMovie::~OldMovie()
- {
- dprintf("end OldMovie\n");
- mvClose(_movieID);
- if(_movieName)free (_movieName);
- _movieName = NULL;
- if(_compressType) free(_compressType);
- _compressType = NULL;
- }
-
-